package com.aforce3.trailertracer.scenes;

import javax.media.j3d.Transform3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.aforce3.trailertracer.Material;
import com.aforce3.trailertracer.Ray;
import com.aforce3.trailertracer.optimizations.BoundingBox;
import com.aforce3.trailertracer.optimizations.SceneBoundingBox;

public class Rectangle extends Primitive
{
	Triangle t1;
	Triangle t2;
	SceneBoundingBox boundingBox;
	
	public Rectangle(Point3d upperLeft, Point3d upperRight, Point3d lowerRight, Point3d lowerLeft)
	{
		t1 = new Triangle(new Point3d(upperLeft), new Point3d(upperRight), new Point3d(lowerLeft));
		t2 = new Triangle(new Point3d(lowerRight), new Point3d(upperRight), new Point3d(lowerLeft));
		
		
	}
	
	public Vector3d getNormal(Ray ray)
	{
		Vector3d n1 = t1.getNormal(ray);
		if(n1 != null) return n1;

		Vector3d n2 = t2.getNormal(ray);
		if(n2 != null) return n2;
		
		return null;
	}

	public Color3f getColor(Ray ray, Scene child)
	{
		Color3f n1 = t1.getColor(ray, child);
		if(n1 != null) return n1;

		Color3f n2 = t2.getColor(ray, child);
		if(n2 != null) return n2;
		
		return null;
	}

	public float getDistance(Ray ray)
	{
		float n1 = t1.getDistance(ray);
		if(n1 > 0) return n1;

		float n2 = t2.getDistance(ray);
		if(n2 > 0) return n2;
		
		return -1;
	}

	@Override
	public Material getMaterial()
	{
		return t1.getMaterial();
	}


	@Override
	public void setMaterial(Material m)
	{
		t1.setMaterial(m);
		t2.setMaterial(m);
	}

	@Override
	protected void applyTransform(Transform3D t)
	{
		t1.applyTransform(t);
		t2.applyTransform(t);
	}

	@Override
	public void applyTransform()
	{
		t1.applyTransform();
		t2.applyTransform();
	}
	
	public BoundingBox getBoundingBox()
	{
		boundingBox = (SceneBoundingBox) t1.getBoundingBox();
		boundingBox.addBox(t2.getBoundingBox());
		boundingBox.setScene(this);
		return boundingBox;
	}
}
